package com.yuanchu.store.controller;

import com.yuanchu.common.utils.R;
import com.yuanchu.common.validator.ValidationGroups;
import com.yuanchu.store.dto.EditAddressDto;
import com.yuanchu.store.entity.YoshopUserAddressEntity;
import com.yuanchu.store.service.YoshopUserAddressService;
import com.yuanchu.store.utils.SecurityUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @version: 1.0
 * @Author: adan
 * @description:
 * @date: 2024-02-04 17:00
 */
@Api(value = "UserAddressController", tags = {"用户地址"})
@RestController
@RequestMapping("shop/address")
public class ShopUserAddressController {

    @Resource
    private YoshopUserAddressService userAddressService;

    Integer storeId = 10001;

    //查询用户地址
    @ApiOperation("查询用户地址")
    @GetMapping("/list")
    public R queryUserAddress() {
        SecurityUtil.YoshopStoreUserEntity user = SecurityUtil.getUser();
        Integer userId = user.getStoreUserId();
        List<YoshopUserAddressEntity> list = userAddressService.queryUserAddress(userId, storeId);
        Map<String, Object> map = new HashMap<>();
        map.put("list", list);
        return R.ok().put("data", map);
    }

    @ApiOperation("查询用户默认地址id")
    @GetMapping("/defaultId")
    public R queryUserDefaultAddressId() {
        Integer userId = SecurityUtil.getUserId();
        Integer defaultId = userAddressService.queryUserDefaultAddressId(userId);
        Map<String, Object> map = new HashMap<>();
        map.put("defaultId", defaultId);
        return R.ok().put("data", map);
    }

    @ApiOperation("查询用户默认地址")
    @GetMapping("/default")
    public YoshopUserAddressEntity queryUserDefaultAddress(@RequestParam("userId") Integer userId, @RequestParam("addressId") Integer addressId) {
        return userAddressService.queryUserDetailAddress(userId, addressId);
    }

    @ApiOperation("设置用户默认地址")
    @PostMapping("/setDefault")
    public R queryUserDefaultAddress(@RequestBody() @Validated({ValidationGroups.Update.class}) EditAddressDto params) {
        Integer userId = SecurityUtil.getUserId();
        Integer storeId = 10001;
        boolean flag = userAddressService.setUserDefaultAddress(userId, Integer.valueOf(params.getAddressId()), storeId);
        if (!flag) {
            return R.error("设置失败");
        }
        return R.ok("设置成功");
    }

    @ApiOperation("查询用户地址详情")
    @GetMapping("/detail")
    public R queryUserDetailAddress(@RequestParam Integer addressId) {
        Integer userId = SecurityUtil.getUserId();
        YoshopUserAddressEntity address = userAddressService.queryUserDetailAddress(userId, addressId);
        Map<String, Object> map = new HashMap<>();
        map.put("detail", address);
        return R.ok().put("data", map);
    }

    @ApiOperation("编辑用户地址")
    @PostMapping("/edit")
    public R editUserAddress(@RequestBody EditAddressDto addressForm) {
        Integer userId = SecurityUtil.getUserId();
        boolean editUserAddress = userAddressService.editUserAddress(userId, addressForm);
        if (!editUserAddress) {
            return R.error("编辑失败");
        }
        return R.ok("编辑成功");
    }

    @ApiOperation("新增用户地址")
    @PostMapping("/add")
    public R addUserAddress(@RequestBody EditAddressDto params) {
        SecurityUtil.YoshopStoreUserEntity user = SecurityUtil.getUser();
        Integer userId = user.getStoreUserId();
        userAddressService.addUserAddress(userId, params);
        return R.ok("添加成功");
    }

    @ApiOperation("编辑用户地址")
    @PostMapping("/remove")
    public R removeUserAddress(@RequestBody @Validated({ValidationGroups.Delete.class}) EditAddressDto addressForm) {
        Integer userId = SecurityUtil.getUserId();
        int storeId = 10001;
        boolean flag = userAddressService.removeUserAddress(userId, addressForm, storeId);
        if (!flag){
            return R.error("删除失败");
        }
        return R.ok("删除成功");
    }

}
